<?php

namespace app\model;

use think\Model;
use think\model\relation\HasOne;

class UserBalanceDetail extends Model
{
    public function lst(): array
    {
        $param = request()->param();
        $data = $this->with(['user'])->when(!empty($param['uid']), function ($query) use ($param) {
            $query->where('uid', $param['uid']);
        })->when(!empty($param['create_time']), function ($query) use ($param) {
            $query->whereBetween("create_time", [$param['create_time'] . " 00:00:00", $param['create_time'] . " 23:59:59"]);
        })->when(!empty($param['username']) || !empty($param['car_number']), function ($query) use ($param) {
            $uid = User::when(!empty($param['use']),function ($q) use ($param){
                $q->where('username',$param['username']);
            })->when(!empty($param['car_number']),function ($q) use ($param){
                $q->where('car_number',$param['car_number']);
            })->column("id");
            $query->whereIn('uid', $uid);
        })->when(!empty($param['tag']),function ($query) use ($param){
            $query->where('tag',$param['tag']);
        })->order(['create_time' => 'desc'])->paginate(['list_rows' => 30, 'page' => $param['page'] ?? 1])->toArray();
        return pageData($data);
    }

    public function user(): HasOne
    {
        return $this->hasOne(User::class, "id", "uid");
    }
}
